package com.dstz.crm.rest.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dstz.base.api.aop.annotion.CatchErr;
import com.dstz.base.api.exception.BusinessMessage;
import com.dstz.base.api.query.FieldLogic;
import com.dstz.base.api.query.FieldRelation;
import com.dstz.base.api.query.QueryFilter;
import com.dstz.base.api.query.QueryOP;
import com.dstz.base.api.response.impl.ResultMsg;
import com.dstz.base.core.util.BeanUtils;
import com.dstz.base.core.util.StringUtil;
import com.dstz.base.db.model.page.PageResult;
import com.dstz.base.db.model.query.DefaultFieldLogic;
import com.dstz.base.db.model.query.DefaultQueryField;
import com.dstz.base.rest.BaseController;
import com.dstz.crm.core.manager.CustomerContractManager;
import com.dstz.crm.core.manager.CustomerManager;
import com.dstz.crm.core.model.Customer;
import com.dstz.crm.core.model.CustomerContract;
import com.dstz.sys.util.ContextUtil;
import com.github.pagehelper.Page;



/**
 * 案例 控制器类<br/>
 * @author  aschs
 * </pre>
 */
@RestController
@RequestMapping("/crm/customerContract")
public class CustomerContractController extends BaseController<CustomerContract>{
	@Resource
	CustomerContractManager customerContractManager;
	
	/**
     * 保存合同表信息
     */
//    @RequestMapping("save")
//    @Override
//    @CatchErr
//    public ResultMsg<String> save( @RequestBody CustomerContract customerContract) throws Exception {
//
//        if (customerContractManager.isCustomerContractExist(customerContract)) throw new BusinessMessage("合同编号在系统中已存在!");
//        
//        customerContractManager.saveCustomerContract(customerContract);
//        return getSuccessResult(customerContract.getId(), "保存成功");
//    }
    
	@Override
	protected String getModelDesc() {
		return "合同";
	}
	
	/**
     * 分页列表
     */
    @RequestMapping("listJson")
    public PageResult listJson(HttpServletRequest request, HttpServletResponse response) throws Exception {
        QueryFilter queryFilter = getQueryFilter(request);
        queryFilter.addFilter("audit_","1", QueryOP.EQUAL);
        Page<CustomerContract> pageList = (Page<CustomerContract>) customerContractManager.query(queryFilter);
        return new PageResult(pageList);
    }
    /**
     * 分页列表
     */
    @RequestMapping("myListJson")
    public PageResult myListJson(HttpServletRequest request, HttpServletResponse response) throws Exception {
    	QueryFilter queryFilter = getQueryFilter(request);
    	String customerId = request.getParameter("customerId");
        if(StringUtil.isNotEmpty(customerId)) {
        	queryFilter.addFilter("customer_id_", customerId, QueryOP.EQUAL);
        }
    	queryFilter.addFilter("audit_","1", QueryOP.EQUAL);
    	queryFilter.addParamsFilter("possesser", ContextUtil.getCurrentUserId());
    	Page<CustomerContract> pageList = (Page<CustomerContract>) customerContractManager.query(queryFilter);
    	return new PageResult(pageList);
    }
    

    /**
     * 批量删除
     */
    @RequestMapping("remove")
    @CatchErr
    public ResultMsg<String> remove(@RequestParam String id) throws Exception {
         String[] aryIds = StringUtil.getStringAryByStr(id);
         customerContractManager.removeByIds(aryIds);
         return getSuccessResult(String.format("删除%s成功", getModelDesc()));
    }
}
